import {
  getDefaultAddress,
  addSforder,
  getToken,
  goDelivery,
  getPlatformAddress
} from "../../../api.js"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list_id: '',
    tab_list: 1,
    time_day: ['今天', '明天', '后天'],
    timeDat_index: 0,
    times: [],
    timeHour_index: 0,
    add_data: [],
    courierNumber: '',
    loo_pic: '',
    api_pic: '',
    token_pic: '',
    business_add: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      list_id: options.id
    })
    getPlatformAddress(
      getApp().globalData.Store.token
    ).then(res => {
      if (res.data.code == 666) {
        this.setData({
          business_add: res.data.data
        })
      }
    })
    this.add()
    var hour = new Date().getHours()
    var h = hour < 8 ? 8 : hour
    var that = this
    for (var i = h; i < 20; i++) {
      var timeTwo = {}
      var th = i < 10 ? '0' + i : i
      timeTwo.time = th + ':00 ~ ' + (th + 1) + ':00'
      timeTwo.th = i
      that.data.times.push(timeTwo)
      if (i == 19) {
        that.setData({
          times: that.data.times
        })
      }
    }
  },
  // 获取默认地址
  add() {
    var that = this
    getDefaultAddress(
      getApp().globalData.Store.token
    ).then(res => {
      if (res.data.code == 666) {
        console.log(res)
        that.setData({
          add_data: res.data.data
        })
      }
    })
  },
  bindPickerday(e) {
    var index = e.detail.value
    var that = this
    this.setData({
      timeDat_index: index
    })
    this.data.times = []
    if (index == 0) {
      var hour = new Date().getHours()
      var h = hour < 8 ? 8 : hour
      for (var i = h; i < 20; i++) {
        var timeTwo = {}
        var th = i < 10 ? '0' + i : i
        timeTwo.time = th + ':00:00 ~ ' + (i + 1 < 10 ? '0' + (i + 1) : i + 1) + ':00:00'
        timeTwo.th = i
        that.data.times.push(timeTwo)
        if (i == 19) {
          that.setData({
            times: that.data.times
          })
        }
      }
    } else {
      for (var i = 8; i < 20; i++) {
        var timeTwo = {}
        var th = i < 10 ? '0' + i : i
        timeTwo.time = th + ':00:00 ~ ' + (i + 1 < 10 ? '0' + (i + 1) : i + 1) + ':00:00'
        timeTwo.th = i
        that.data.times.push(timeTwo)
        if (i == 19) {
          that.setData({
            times: that.data.times
          })
        }
      }
    }
  },
  bindPickerhour(e) {
    var index = e.detail.value
    this.setData({
      timeHour_index: index
    })
  },
  // 地址列表
  addlis(e) {
    wx.navigateTo({
      url: '/pages/myList/addList/addList?stateTyp=1',
    })
  },
  tab_list_ty(e) {
    var num = e.currentTarget.dataset.type
    var that = this
    if (num == that.data.tab_list) {
      return
    } else {
      that.setData({
        tab_list: num
      })
    }
  },
  // 扫描单号
  scanning(e) {
    var that = this
    wx.showLoading({
      title: '正在扫描',
    })
    wx.scanCode({
      success(res) {
        wx.hideLoading()
        wx.showToast({
          title: '扫描成功',
        })
        that.setData({
          courierNumber: res.result
        })
      }, fail() {
        wx.hideLoading()
        wx.showToast({
          title: '扫描失败',
          icon: 'none'
        })
      }
    })
  },
  up_pic(e) {
    var that = this
    wx.showLoading({
      title: '正在上传',
    })
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        getToken().then(res => {
          if (res.data.code == 666) {
            that.setData({
              token_pic: res.data.data.token,
            })
            var timestamp = Date.parse(new Date())
            var type = tempFilePaths[0].substring(tempFilePaths[0].lastIndexOf("."), tempFilePaths[0].length);
            var keyy = timestamp + type
            wx.uploadFile({
              url: 'https://upload.qiniup.com',
              filePath: tempFilePaths[0],
              name: 'file', //文件的key 
              header: {
                "Content-Type": "multipart/form-data"
              },
              formData: {
                token: that.data.token_pic,
                key: keyy
              }, //传递参数
              success: function (res) {
                wx.hideLoading()
                var data = JSON.parse(res.data)
                that.data.loo_pic = tempFilePaths
                that.data.api_pic = data.key
                that.setData({
                  loo_pic: that.data.loo_pic,
                  api_pic: that.data.api_pic
                })
              }
            })
          }
        })
      },
      fail() {
        wx.hideLoading()
      }
    })
  },
  submit(e) {
    var that = this
    if (that.data.tab_list == 1) {
      if (!that.data.add_data) {
        wx.showToast({
          title: '请选择地址',
          icon: 'none'
        })
        return
      }
      wx.showLoading({
        title: '正在预约',
      })
      var now = Date.parse(new Date())
      if (that.data.timeDat_index == 1) {
        now = now + 24 * 60 * 60 * 1000
      } else if (that.data.timeDat_index == 2) {
        now = now + 24 * 60 * 60 * 2 * 1000
      }
      var data_time = that.data.times[that.data.timeHour_index].th
      var api_data = data_time < 10 ? '0' + data_time : data_time
      var tomorrow_date = new Date(now)
      var Y_tomorrow = tomorrow_date.getFullYear();
      var M_tomorrow = (tomorrow_date.getMonth() + 1 < 10 ? '0' + (tomorrow_date.getMonth() + 1) : tomorrow_date.getMonth() + 1);
      var D_tomorrow = tomorrow_date.getDate() < 10 ? '0' + tomorrow_date.getDate() : tomorrow_date.getDate();
      var api_data = Y_tomorrow + '-' + M_tomorrow + '-' + D_tomorrow + ' ' + api_data + ":00:00"
      addSforder(
        getApp().globalData.Store.token,
        that.data.list_id,
        that.data.add_data.id,
        api_data
      ).then(res => {
        if (res.data.code == 666) {
          wx.hideLoading()
          wx.showToast({
            title: '预约成功',
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 2
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      })
      return
    } else if (that.data.tab_list == 2) {
      var form = e.detail.value
      if (!form.express) {
        wx.showToast({
          title: '请填写快递公司',
          icon: 'none'
        })
        return
      }
      if (!form.courierNumber) {
        wx.showToast({
          title: '请填写快递单号',
          icon: 'none'
        })
        return
      }
      goDelivery(
        getApp().globalData.Store.token,
        that.data.list_id,
        form.express,
        form.courierNumber,
        that.data.api_pic
      ).then(res => {
        console.log(res)
        wx.hideLoading()
        wx.showToast({
          title: '提交成功',
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 2
          })
        }, 1500)
      })
    }
  },
  copy(e) {
    var that = this.data.business_add
    wx.setClipboardData({
      data: that.province_name + that.city_name + that.area_name + that.resell_address + that.resell_name + that.resell_phone,
      success: function () {
        wx.showToast({
          title: '复制成功',
        })
      }
    })
  },
  onShow: function () {
    getApp().startpage(new Date().getTime())
  },
  onHide() {
    getApp().endpage(new Date().getTime())
  },
})